package com.tarena.lbs.coupon.dao.repository.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.coupon.dao.mapper.UserCouponsMapper;
import com.tarena.lbs.coupon.dao.repository.UserCouponsRepository;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class UserCouponsRepositoryImpl implements UserCouponsRepository {
    @Autowired
    private UserCouponsMapper userCouponsMapper;
    @Override
    public void save(UserCouponsPO userCouponsPO) {
        log.info("保存用户优惠券:{}",userCouponsPO);
        userCouponsMapper.insert(userCouponsPO);
    }

    @Override
    public UserCouponsPO findByUserIdAndCouponId(Integer userId, Integer couponId) {
        QueryWrapper<UserCouponsPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("coupon_id",couponId);
        return userCouponsMapper.selectOne(queryWrapper);

    }

    @Override
    public void update(UserCouponsPO userCouponsPO) {
        userCouponsMapper.updateById(userCouponsPO);
    }

    @Override
    public void delete(UserCouponsPO userCouponsPO) {
        userCouponsMapper.deleteById(userCouponsPO);
    }

    @Override
    public Long countReceiveCoupons(Integer couponId, Integer userId) {
        log.info("检查用户userId:{}领取优惠券couponId:{}数量", userId, couponId);
        QueryWrapper<UserCouponsPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("coupon_id",couponId);
        log.info("检查");
        Long count = userCouponsMapper.selectCount(queryWrapper);
        log.info("用户userId:{}领取优惠券couponId:{}数量:{}", userId, couponId, count);
        return count;
    }

    @Override
    public UserCouponsPO findByCouponCode(String couponCode) {
        QueryWrapper<UserCouponsPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("coupon_code",couponCode);
        return userCouponsMapper.selectOne(queryWrapper);
    }

    @Override
    public PageInfo<UserCouponsPO> pagesList(UserCouponQuery query) {
        log.info("查询用户领取优惠券列表, query={}", query);
        // 使用PageHelper进行分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());

        // 构建查询条件
        QueryWrapper<UserCouponsPO> queryWrapper = assembleUserCouponQuery(query);

        // 执行查询
        List<UserCouponsPO> list = userCouponsMapper.selectList(queryWrapper);

        // 包装成PageInfo返回
        log.info("查询结果list={}", list);
        return new PageInfo<>(list);
    }

    // 辅助方法：组装查询条件
    private QueryWrapper<UserCouponsPO> assembleUserCouponQuery(UserCouponQuery query) {
        QueryWrapper<UserCouponsPO> queryWrapper = new QueryWrapper<>();

        // 根据用户ID查询
        if (query.getUserId() != null) {
            queryWrapper.eq("user_id", query.getUserId());
        }

        // 根据优惠券状态查询
        if (query.getStatus() != null) {
            queryWrapper.eq("status", query.getStatus());
        }

        // 根据订单编号查询
        if (StringUtils.isNotBlank(query.getOrderNo())) {
            queryWrapper.eq("order_no", query.getOrderNo());
        }

        // 根据投放渠道查询
        if (query.getReceiveChannel() != null) {
            queryWrapper.eq("receive_channel", query.getReceiveChannel());
        }

        // 根据优惠券类型查询
        if (query.getCouponType() != null) {
            queryWrapper.eq("coupon_type", query.getCouponType());
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc("create_at");

        return queryWrapper;
    }
}
